package components;

import game.Multiplayer;

import java.util.Timer;
import java.util.TimerTask;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/**
 * Implements the Item with the different effects
 *
 */
public class Item extends Field{

	/**
	 * Information about the sort of item and the following effect
	 */
	public String sort;
	
	/**
	 * Is used to handle time
	 */
	final transient Timer timer = new Timer();
	
	/**
	 * Defines the type of the disease
	 */
	String disease;
	
	/**
	 * Saves the normal status of the player
	 */
	static float value;

	/**
	 * Standard constructor
	 */
	public Item(float[] position, String sort, Image img) {

		super(position, true, false, true, img, "item");
		// TODO Auto-generated constructor stub
		this.sort=sort;
		this.disease=null;

	}
	
	/**
	 * Decides the effect and changes the status of the player
	 */
	public void effect(final int bomber){
		if(this.sort.equals("fire_up")){
			Multiplayer.map.bomber[bomber].range++;
		}
		else if(this.sort.equals("bomb_up")){
			Multiplayer.map.bomber[bomber].bombLimit++;
			System.out.println(Multiplayer.map.bomber[bomber].bombLimit);
		}
		else if(this.sort.equals("speed_up")){
			Multiplayer.map.bomber[bomber].speed+=0.0005f;
		}
		else if(this.sort.equals("kick")){
			Multiplayer.map.bomber[bomber].kick=true;
		}
		else if(this.sort.equals("power")){
			Multiplayer.map.bomber[bomber].range=20;
		}
		else if(this.sort.equals("life")){
			Multiplayer.map.bomber[bomber].lifes++;
		}
		else if(this.sort.equals("invincible")){
			Multiplayer.map.bomber[bomber].invincible=true;
			Multiplayer.map.bomber[bomber].paint(1);
			
			Multiplayer.map.bomber[bomber].timer.cancel();
			Multiplayer.map.bomber[bomber].timer=new Timer();
			Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
				public void run() {
					Multiplayer.map.bomber[bomber].invincible=false;
					Multiplayer.map.bomber[bomber].unpaint();
				};
			}, 15000);
			
		}
		else if(this.sort.equals("disease")){
			
			if(Multiplayer.map.bomber[bomber].item != null){
				Multiplayer.map.bomber[bomber].item.finishEffect(bomber);
			}
			Multiplayer.map.bomber[bomber].item=this;
			Multiplayer.map.bomber[bomber].paint(0);

			double number = Math.random()*9;

			if(number<=1){
				this.disease="speed+";
				value=Multiplayer.map.bomber[bomber].speed;
				Multiplayer.map.bomber[bomber].speed=0.025f;
				Multiplayer.map.bomber[bomber].timer.cancel();
				Multiplayer.map.bomber[bomber].timer=new Timer();
				Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
					public void run() {
						Multiplayer.map.bomber[bomber].speed=value;
						Multiplayer.map.bomber[bomber].item=null;
						Multiplayer.map.bomber[bomber].unpaint();
					};
				}, 15000);
			}
			else if(number<=2){
				this.disease="range";
				value=Multiplayer.map.bomber[bomber].range;
				Multiplayer.map.bomber[bomber].range=1;
				Multiplayer.map.bomber[bomber].timer.cancel();
				Multiplayer.map.bomber[bomber].timer=new Timer();
				Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
					public void run() {
						Multiplayer.map.bomber[bomber].range=(int)value;
						Multiplayer.map.bomber[bomber].item=null;
						Multiplayer.map.bomber[bomber].unpaint();
					};
				}, 15000);
			}
			else if(number<=3){
				this.disease="time";
				value=Multiplayer.map.bomber[bomber].time;
				Multiplayer.map.bomber[bomber].time=1000;
				Multiplayer.map.bomber[bomber].timer.cancel();
				Multiplayer.map.bomber[bomber].timer=new Timer();
				Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
					public void run() {
						Multiplayer.map.bomber[bomber].time=(int)value;
						Multiplayer.map.bomber[bomber].item=null;
						Multiplayer.map.bomber[bomber].unpaint();
					};
				}, 15000);
			}
			else if(number<=4){
				this.disease="speed-";
				value=Multiplayer.map.bomber[bomber].speed;
				Multiplayer.map.bomber[bomber].speed=0.0017f;
				Multiplayer.map.bomber[bomber].timer.cancel();
				Multiplayer.map.bomber[bomber].timer=new Timer();
				Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
					public void run() {
						Multiplayer.map.bomber[bomber].speed=value;
						Multiplayer.map.bomber[bomber].item=null;
						Multiplayer.map.bomber[bomber].unpaint();
					};
				}, 15000);
			}
			else if(number<=5){
				this.disease="forcedToBomb";
				Multiplayer.map.bomber[bomber].forcedToBomb=true;
				Multiplayer.map.bomber[bomber].timer.cancel();
				Multiplayer.map.bomber[bomber].timer=new Timer();
				Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
					public void run() {
						Multiplayer.map.bomber[bomber].forcedToBomb=false;
						Multiplayer.map.bomber[bomber].item=null;
						Multiplayer.map.bomber[bomber].unpaint();
					};
				}, 15000);
			}
			else if(number<=6){
				this.disease="bombup";
				Multiplayer.map.bomber[bomber].bombLimit++;
				Multiplayer.map.bomber[bomber].timer.cancel();
				Multiplayer.map.bomber[bomber].timer=new Timer();
				Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
					public void run() {
						Multiplayer.map.bomber[bomber].bombLimit--;
						Multiplayer.map.bomber[bomber].item=null;
						Multiplayer.map.bomber[bomber].unpaint();
					};
				}, 15000);
			}
			else if(number<=7){
				this.disease="rangeup";
				Multiplayer.map.bomber[bomber].range+=3;
				Multiplayer.map.bomber[bomber].timer.cancel();
				Multiplayer.map.bomber[bomber].timer=new Timer();
				Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
					public void run() {
						Multiplayer.map.bomber[bomber].range-=3;
						Multiplayer.map.bomber[bomber].item=null;
						Multiplayer.map.bomber[bomber].unpaint();
					};
				}, 15000);
			}
			else if(number<=8){
				this.disease="invinc";
				Multiplayer.map.bomber[bomber].invincible=true;
				Multiplayer.map.bomber[bomber].timer.cancel();
				Multiplayer.map.bomber[bomber].timer=new Timer();
				Multiplayer.map.bomber[bomber].timer.schedule(new TimerTask() {
					public void run() {
						Multiplayer.map.bomber[bomber].invincible=false;
						Multiplayer.map.bomber[bomber].item=null;
						Multiplayer.map.bomber[bomber].unpaint();
					};
				}, 15000);
			}
			
			else if(number<=9){
				System.out.println("tausche");
				this.disease="change";
				double num=Multiplayer.map.bomber[bomber].id;
				int sel=Multiplayer.map.bomber[bomber].id;
				
				while(num>=Multiplayer.map.bomber[bomber].id && num<Multiplayer.map.bomber[bomber].id+1){
				num = Math.random()*game.Multiplayer.map.bomber.length;
				}

				System.out.println(sel);
				for(int i =0; i<game.Multiplayer.map.bomber.length; i++){
					if(num<=(i+1)){
						sel=i;
						break;
					}
				}
				
				float x= game.Multiplayer.map.bomber[sel].position[0];
				float y= game.Multiplayer.map.bomber[sel].position[1];
				
				game.Multiplayer.map.bomber[sel].position[0]= Multiplayer.map.bomber[bomber].position[0];
				game.Multiplayer.map.bomber[sel].position[1]= Multiplayer.map.bomber[bomber].position[1];
				
				Multiplayer.map.bomber[bomber].position[0]= x;
				Multiplayer.map.bomber[bomber].position[1]= y;
				
				Multiplayer.map.bomber[bomber].unpaint();
				Multiplayer.map.bomber[bomber].timer.cancel();
			}

		}

	}
	
	/**
	 * Sets the status of the bomber back to normal, after disease is over
	 * @param bomber Influenced player
	 */
	public void finishEffect(int bomber){
		if(this.disease.equals("speed+") || this.disease.equals("speed-")){
			Multiplayer.map.bomber[bomber].speed=value;
			Multiplayer.map.bomber[bomber].item=null;
			Multiplayer.map.bomber[bomber].unpaint();
		}
		else if(this.disease.equals("range")){
			Multiplayer.map.bomber[bomber].range=(int)value;
			Multiplayer.map.bomber[bomber].item=null;
			Multiplayer.map.bomber[bomber].unpaint();
		}
		else if(this.disease.equals("time")){
			Multiplayer.map.bomber[bomber].time=(int)value;
			Multiplayer.map.bomber[bomber].item=null;
			Multiplayer.map.bomber[bomber].unpaint();
		}
		else if(this.disease.equals("forcedToBomb")){
			Multiplayer.map.bomber[bomber].forcedToBomb=false;
			Multiplayer.map.bomber[bomber].item=null;
			Multiplayer.map.bomber[bomber].unpaint();
		}
		else if(this.disease.equals("bombup")){
			Multiplayer.map.bomber[bomber].bombLimit--;
			Multiplayer.map.bomber[bomber].item=null;
			Multiplayer.map.bomber[bomber].unpaint();
		}
		else if(this.disease.equals("rangeup")){
			Multiplayer.map.bomber[bomber].range-=3;
			Multiplayer.map.bomber[bomber].item=null;
			Multiplayer.map.bomber[bomber].unpaint();
		}
		else if(this.disease.equals("invinc")){
			Multiplayer.map.bomber[bomber].invincible=false;
			Multiplayer.map.bomber[bomber].item=null;
			Multiplayer.map.bomber[bomber].unpaint();
		}
		
	}

}
